package com.longteng.lesson2.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/redirectController")
public class RedirectController {

    @RequestMapping("/bj")
    public ModelAndView m1(String name,RedirectAttributes redirectAttributes){
        //我就是北京市社保局，希白来办理社保业务
        ModelAndView modelAndView=new ModelAndView();
        //我告诉希白，这个业务需要去海淀区社保局办理
        if(name.equalsIgnoreCase("xibai")){
            redirectAttributes.addAttribute("name",name);
            modelAndView.setViewName("redirect:/redirectController/haidian?name="+name);
        }else {
            modelAndView.setViewName("beijing");
        }
        return modelAndView;
    }
    @RequestMapping("/haidian")
    public ModelAndView haidian(String name){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("haidian");
        return modelAndView;
    }
    @RequestMapping("/bj1")
    public ModelAndView bj1(String name){
        //到了北京市社保局，然后社保局内部处理这个业务
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("forward:/redirectController/haidian?name="+name);
        return modelAndView;
    }
    @RequestMapping("/bj2")
    public ModelAndView bj2(String name, HttpServletRequest request, HttpServletResponse response){
        //到了北京市社保局，然后社保局内部处理这个业务
        ModelAndView modelAndView=new ModelAndView();
        try {
            //这种方式是上面那种的底层实现，在没有moedlAdnView的情况，只能使用这种
            //如果没有到控制层的时候，就要用request这种方式了
            request.getRequestDispatcher("/redirectController/haidian?name="+name)
                    .forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        modelAndView.setViewName("forward:/redirectController/haidian?name="+name);
        return modelAndView;
    }
}
